package com.haoshuang.zhouzhoubang.ui.activity

import android.os.Bundle
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter4.BaseQuickAdapter
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityWalletDetailsBinding
import com.haoshuang.zhouzhoubang.databinding.ItemDetailsBinding
import com.haoshuang.zhouzhoubang.model.bean.YuBean
import com.haoshuang.zhouzhoubang.ui.vm.WalletDetailsViewModel
import com.mufeng.libs.base.BaseBindingHolder
import com.mufeng.libs.base.BaseBindingQuickAdapter
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.textColor


/**
 *  保证金明细
 */
class WalletDetailsActivity5 :
    BaseVMActivity<WalletDetailsViewModel, ActivityWalletDetailsBinding>() {
    /**recycleview列表适配器*/
    private val mrvAdapter by lazy {
        object : BaseBindingQuickAdapter<YuBean.Data, ItemDetailsBinding>(mutableListOf()) {

            override fun onBindViewHolder(
                holder: BaseBindingHolder,
                position: Int,
                item: YuBean.Data?
            ) {
                val tvTitle = holder.getView<TextView>(R.id.tvTitle)
                val tvTime = holder.getView<TextView>(R.id.tvTime)
                val tvValue = holder.getView<TextView>(R.id.tvValue)
                tvTitle.text=item?.title
                tvTime.text=item?.add_time
                if ((item?.pm?:0) > 0) {
                    tvValue.textColor = R.color.c_3b76c2
                    tvValue.text = "${item?.number}"
                } else {
                    tvValue.textColor = R.color.c_ff0000
                    tvValue.text = item?.number
                }
            }
        }
    }
    var type=1
    override fun initView(savedInstanceState: Bundle?) {
        val title = intent.getString("title", "")
        //2余额明细 3消费明细 1保证金明细

        when(title){
            "余额明细"->{
                type=2
            }
            "消费记录"->{
                type=3
            }
            else->{
                type=1
            }
        }
        val layoutManager = LinearLayoutManager(this)    // 创建布局管理者

        binding.apply {
            appBar.ivBack.clickWithTrigger { finish() }
            appBar.tvTitle.text = title

            recyclerView.layoutManager = layoutManager    // 给recycleview设置布局管理者
            recyclerView.adapter = mrvAdapter    // 给recycleview设置适配器对象
            mrvAdapter.setEmptyData()
            mrvAdapter.setOnItemClick { view, position, item ->
                val item = mrvAdapter.getItem(position)
                if (item?.type == "team_refund_deposit") return@setOnItemClick
                if (item?.link_id != null && item.link_id != "0" && item.link_id.isNotEmpty()){
                    // todo 订单详情
//                    startPage<ServiceOrderStatusActivity>("ids" to item.link_id.toIntOrNull())
                }
            }
        }
    }

    var list = arrayListOf< YuBean.Data>()
    override fun initData() {

        binding.refreshLayout.setOnRefreshListener {
            p = 1
            getNet()
            binding.refreshLayout.finishRefresh(2000)
            binding.refreshLayout.setEnableLoadMore(true)
        }
        binding.refreshLayout.setOnLoadMoreListener {
            if (list.size < 10) {
                binding.refreshLayout.setEnableLoadMore(false)
            } else {
                p++
                getNet()
            }
            binding.refreshLayout.finishLoadMore(2000)
        }
        p = 1
        getNet()

    }

    private fun getNet() {
        vm.getData("page" to p,"limit" to 10,"type" to 5)
    }

    override fun startObserve() {
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                if (p == 1) {
                    list.clear()

                }
                list.addAll(it?.data!!)
                mrvAdapter.notifyDataSetChanged()
            }
        }
    }

}